/**
 * 场景管理类
 */
class SceneManager {
	//单例
	private static _instance: SceneManager;
	//场景根节点
	public rootLayer: eui.UILayer;
	//当前场景
	private _currentScene: GameObject;
	//当前窗口
	private _currentWindow: GameObject;
	//构造函数
	public constructor() {
		//初始化
		this.init();
	}
	//切换场景
	async changeScene(targetScene) {
		if (this._currentScene) {
			this.rootLayer.removeChild(this._currentScene);
			this._currentScene = null;
		}
		this.rootLayer.addChild(targetScene);
		this._currentScene = targetScene;
	}
	//弹出窗口
	public showWindow(targetWindow) {
		this.closeWindow();
		this._currentWindow = targetWindow;
		this.rootLayer.addChild(targetWindow);
	}
	//关闭当前窗口
	public closeWindow() {
		if (this._currentWindow) {
			this.rootLayer.removeChild(this._currentWindow);
			this._currentWindow = null;
		}
	}
	//获取当前场景
	public getCurrentScene() {
		return this._currentScene;
	}
	//单例
	public static getInstance(): SceneManager {
		if (!this._instance) {
			this._instance = new SceneManager();
		}
		return this._instance;
	}

	//初始化函数
	private init() {
		//stage分辨率跟随设备分辨率调整
		let _stage = egret.MainContext.instance.stage;
		_stage.addEventListener(egret.Event.RESIZE, this.stageResize, this);
		_stage.setContentSize(
			window.innerWidth,
			window.innerHeight
		);
	}
	private stageResize() {
		let _stage = egret.MainContext.instance.stage;
		_stage.removeEventListener(egret.Event.RESIZE, this.stageResize, this);
		_stage.setContentSize(
			window.innerWidth,
			window.innerHeight
		);
		if ((window.innerWidth / window.innerHeight) <= 1) {
			_stage.setContentSize(
				window.innerHeight,
				window.innerWidth
			);
		}
		//console.log(`Window resize:${_stage.height}*${_stage.width}`);
		_stage.addEventListener(egret.Event.RESIZE, this.stageResize, this);
	}
}
